package com.ruoyi.jihuo.api;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class UploadImageApi {

    public String uploadImage(String sourceUrl) {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("picUrl", sourceUrl);
        RequestBody body = RequestBody.create(mediaType, jsonObject.toJSONString());
        Request request = new Request.Builder()
                .url("http://8.142.162.128:30005/sl6mk2xe/api/media/uploadpic")
                .method("POST", body)
                .addHeader("appid", "8000020386")
                .addHeader("appsecret", "epZvPAs8EKmQ54NQGnMa")
                .addHeader("Content-Type", "application/json")
                .build();
        String newPicUrl = null;
        try {
            Response response = client.newCall(request).execute();
            JSONObject respJson = JSONObject.parseObject(response.body().string());
            if (respJson.getJSONObject("content")==null){
                log.error("图片上传失败-" + sourceUrl);
                throw new RuntimeException("图片上传接口失败," + respJson.getString("msg"));
            }
            newPicUrl = respJson.getJSONObject("content").getString("newPicUrl");
        } catch (IOException e) {
            log.error("上传图片失败", e);
            throw new RuntimeException(e);
        }
        return newPicUrl;
    }
    public String uploadLocalImage(String imageBase) {
        if (StringUtils.isBlank(imageBase)) {
            return null;
        }
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("picData", imageBase);
        RequestBody body = RequestBody.create(mediaType, jsonObject.toJSONString());
        Request request = new Request.Builder()
                .url("http://8.142.162.128:30005/sl6mk2xe/api/media/uploadlocalpic")
                .method("POST", body)
                .addHeader("appid", "8000020386")
                .addHeader("appsecret", "epZvPAs8EKmQ54NQGnMa")
                .addHeader("Content-Type", "application/json")
                .build();
        String newPicUrl = null;
        try {
            Response response = client.newCall(request).execute();
            JSONObject respJson = JSONObject.parseObject(response.body().string());
            if ( respJson.getString("code").equals("000000")){
                newPicUrl = respJson.getJSONObject("content").getString("newPicUrl");
            }else {
                throw new RuntimeException(respJson.getString("msg"));
            }
        } catch (IOException e) {
            log.error("上传图片失败", e);
            throw new RuntimeException(e);
        }
        return newPicUrl;
    }
}
